/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Configuracionfinger.java
 *
 * Created on 10-feb-2013, 16:05:27
 */
package sisvoto.vista;

import SecuGen.FDxSDK.jni.FDxDeviceInfoParam;
import SecuGen.FDxSDK.jni.FDxDeviceName;
import SecuGen.FDxSDK.jni.FDxError;
import SecuGen.FDxSDK.jni.FDxPortAddr;
import SecuGen.FDxSDK.jni.JFPLib;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;


/*
 *
 * @author Dianita
 */
public class Configuracionfinger extends javax.swing.JDialog {

    private long nombreDispositivo;
    private long puertoDispositivo;
    private long ret;
    private static JFPLib fplib = null;
    private FDxDeviceInfoParam deviceInfo = new FDxDeviceInfoParam();
    private BufferedImage imgRegistro;
    private boolean capturado = false;
    private boolean led = false;
    private byte[] remgin = new byte[400];

    /** Creates new form Configuracionfinger */
    public Configuracionfinger(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        if (fplib == null) {
            desactivarBotones();
        } else {
            activarBotones();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        imagePanel1 = new com.mycsistemas.swingec.panel.ImagePanel();
        curvesPanel1 = new com.mycsistemas.swingec.panel.CurvesPanel();
        jLblPartidoElectoral = new javax.swing.JLabel();
        jcbDispositivo = new javax.swing.JComboBox();
        jLblAlias2 = new javax.swing.JLabel();
        txtSalida = new javax.swing.JTextField();
        btnLed = new javax.swing.JButton();
        jLblAlias4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        lblImagen = new javax.swing.JLabel();
        btnIniciar = new javax.swing.JButton();
        btnCaptura = new javax.swing.JButton();
        jLblAlias5 = new javax.swing.JLabel();
        jLblAlias6 = new javax.swing.JLabel();
        jcbContraste = new javax.swing.JComboBox();
        jcbBrillo = new javax.swing.JComboBox();
        btnfinalizar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Configuración Dispositivo Biométrico");

        imagePanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/''_10102012_1738.jpeg"))); // NOI18N

        curvesPanel1.setLayout(null);

        jLblPartidoElectoral.setFont(new java.awt.Font("Georgia", 3, 18));
        jLblPartidoElectoral.setForeground(new java.awt.Color(255, 255, 255));
        jLblPartidoElectoral.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLblPartidoElectoral.setText("Configuración de Dispositivo");
        curvesPanel1.add(jLblPartidoElectoral);
        jLblPartidoElectoral.setBounds(60, 30, 320, 30);

        jcbDispositivo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "USB", "Parallel", "CN_FDU01", "CN_FDP01", "CN_FDU02", "CN_FDP02" }));
        curvesPanel1.add(jcbDispositivo);
        jcbDispositivo.setBounds(140, 70, 260, 30);

        jLblAlias2.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblAlias2.setForeground(new java.awt.Color(255, 255, 255));
        jLblAlias2.setText("Salida:");
        curvesPanel1.add(jLblAlias2);
        jLblAlias2.setBounds(40, 360, 70, 30);

        txtSalida.setEditable(false);
        curvesPanel1.add(txtSalida);
        txtSalida.setBounds(110, 360, 290, 30);

        btnLed.setText("LED DEL DISPOSITVO");
        btnLed.setToolTipText("Cerrar Conexión con el Dispositivo");
        btnLed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLedActionPerformed(evt);
            }
        });
        curvesPanel1.add(btnLed);
        btnLed.setBounds(220, 200, 170, 30);

        jLblAlias4.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblAlias4.setForeground(new java.awt.Color(255, 255, 255));
        jLblAlias4.setText("Contraste:");
        curvesPanel1.add(jLblAlias4);
        jLblAlias4.setBounds(220, 280, 110, 30);

        lblImagen.setToolTipText("Huella Dactilar");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblImagen, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(lblImagen, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                .addContainerGap())
        );

        curvesPanel1.add(jPanel1);
        jPanel1.setBounds(30, 120, 180, 230);

        btnIniciar.setText("INICIAR CONEXIÓN");
        btnIniciar.setToolTipText("Cerrar Conexión con el Dispositivo");
        btnIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIniciarActionPerformed(evt);
            }
        });
        curvesPanel1.add(btnIniciar);
        btnIniciar.setBounds(220, 120, 170, 30);

        btnCaptura.setText("CAPTURA DE PRUEBA");
        btnCaptura.setToolTipText("Cerrar Conexión con el Dispositivo");
        btnCaptura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCapturaActionPerformed(evt);
            }
        });
        curvesPanel1.add(btnCaptura);
        btnCaptura.setBounds(220, 160, 170, 30);

        jLblAlias5.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblAlias5.setForeground(new java.awt.Color(255, 255, 255));
        jLblAlias5.setText("Dispositivo :");
        curvesPanel1.add(jLblAlias5);
        jLblAlias5.setBounds(30, 70, 110, 30);

        jLblAlias6.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblAlias6.setForeground(new java.awt.Color(255, 255, 255));
        jLblAlias6.setText("Brillo:");
        curvesPanel1.add(jLblAlias6);
        jLblAlias6.setBounds(220, 240, 80, 30);

        jcbContraste.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100" }));
        curvesPanel1.add(jcbContraste);
        jcbContraste.setBounds(300, 280, 90, 30);

        jcbBrillo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100" }));
        curvesPanel1.add(jcbBrillo);
        jcbBrillo.setBounds(300, 240, 90, 30);

        btnfinalizar.setText("FINALIZAR CONEXIÓN");
        btnfinalizar.setToolTipText("Cerrar Conexión con el Dispositivo");
        btnfinalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnfinalizarActionPerformed(evt);
            }
        });
        curvesPanel1.add(btnfinalizar);
        btnfinalizar.setBounds(220, 320, 170, 30);

        javax.swing.GroupLayout imagePanel1Layout = new javax.swing.GroupLayout(imagePanel1);
        imagePanel1.setLayout(imagePanel1Layout);
        imagePanel1Layout.setHorizontalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(curvesPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)
        );
        imagePanel1Layout.setVerticalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(curvesPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 442, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIniciarActionPerformed
// TODO add your handling code here:
    int dispositivoseleccionado = jcbDispositivo.getSelectedIndex();
    dispSelect(dispositivoseleccionado);
    fplib = new JFPLib(this.nombreDispositivo);
    if ((fplib != null) && (fplib.jniLoadStatus == FDxError.ERROR_NONE)) {
        this.txtSalida.setText("JFPLib Inicializado con Éxito");
        this.puertoDispositivo = FDxPortAddr.AUTO_DETECT;
        ret = fplib.openDevice(puertoDispositivo);
        if (ret == FDxError.ERROR_NONE) {
            this.txtSalida.setText("Dispositivo Conectado.");
            ret = fplib.getDeviceInfo(deviceInfo);
            if (ret == FDxError.ERROR_NONE) {
                imgRegistro = new BufferedImage(deviceInfo.imageWidth, deviceInfo.imageHeight, BufferedImage.TYPE_BYTE_GRAY);
                activarBotones();
                this.jcbBrillo.setSelectedIndex(deviceInfo.brightness);
                this.jcbContraste.setSelectedIndex(deviceInfo.contrast);
            } else {
                this.txtSalida.setText("ERROR: Información de Dispositivo");
            }
        } else {
            this.txtSalida.setText("ERROR: No es Posibe Conectar el Dispositivo");
        }
    } else {
        this.txtSalida.setText("JFPLib No se ha Inicializado");
    }



}//GEN-LAST:event_btnIniciarActionPerformed

private void btnfinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnfinalizarActionPerformed
// TODO add your handling code here:
    long iError;
    iError = fplib.close();
    if (iError == FDxError.ERROR_NONE) {
        this.txtSalida.setText("Conexión con el Dispotivo Cerrada.");
        this.desactivarBotones();
    } else {
        this.txtSalida.setText("ERROR: La Conexión no Pudo Cerrarse.");
    }

}//GEN-LAST:event_btnfinalizarActionPerformed

private void btnLedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLedActionPerformed
// TODO add your handling code here:
    if (fplib != null) {
        ret = fplib.setLedOn(led);
        if (ret == FDxError.ERROR_NONE) {
            if (led == true) {
                this.txtSalida.setText("Led Encedido");
            } else {
                this.txtSalida.setText("Led Apagado");
            }
        }
        led = !led;
    } else {
        this.txtSalida.setText("JFPLib No Ha sido Inicializada.");
    }
}//GEN-LAST:event_btnLedActionPerformed

private void btnCapturaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCapturaActionPerformed
// TODO add your handling code here:

    BufferedImage img1gray = new BufferedImage(260, 300, BufferedImage.TYPE_BYTE_GRAY);
    byte[] imageBuffer1 = ((java.awt.image.DataBufferByte) img1gray.getRaster().getDataBuffer()).getData();
    if (fplib != null) {
        led = !led;
        fplib.setBrightness(jcbBrillo.getSelectedIndex());
        fplib.setContrast(jcbContraste.getSelectedIndex());
        ret = fplib.getImage(imageBuffer1);
        if (ret == FDxError.ERROR_NONE) {
            this.lblImagen.setIcon(new ImageIcon(img1gray));
            this.txtSalida.setText("Imagen Obtenida Exitosamente");

            /*
            fplib.getMinutiae(imageBuffer1, remgin);
            sisvoto.modelo.HuellaDactilar d = (((ArrayList<sisvoto.modelo.HuellaDactilar>) new ConsultasBD().buscarPOR("HuellaDactilar", "elector", "'4'")).get(0));
            byte[] remgin2 = d.getHuella();

            if (FDxError.ERROR_NONE == fplib.matchForVerification(remgin, remgin2, FDxSecurityLevel.L5_NORMAL)) {
                new Mensajes().StandarTick(this,"son iguales");
            }
             */  

        } else {
            this.txtSalida.setText("No se pudo Obtener la Imagen");
        }
    } else {
        this.txtSalida.setText("JFPLib No Ha Sido Inicializada");
    }
}//GEN-LAST:event_btnCapturaActionPerformed

    /**
     * @param args the command line arguments
     */


    public FDxDeviceInfoParam getDeviceInfo() {
        return deviceInfo;
    }

    public void setDeviceInfo(FDxDeviceInfoParam deviceInfo) {
        this.deviceInfo = deviceInfo;
    }

    public static JFPLib getFplib() {
        return fplib;
    }

    public static void setFplib(JFPLib fplib) {
        Configuracionfinger.fplib = fplib;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCaptura;
    private javax.swing.JButton btnIniciar;
    private javax.swing.JButton btnLed;
    private javax.swing.JButton btnfinalizar;
    private com.mycsistemas.swingec.panel.CurvesPanel curvesPanel1;
    private com.mycsistemas.swingec.panel.ImagePanel imagePanel1;
    private javax.swing.JLabel jLblAlias2;
    private javax.swing.JLabel jLblAlias4;
    private javax.swing.JLabel jLblAlias5;
    private javax.swing.JLabel jLblAlias6;
    private javax.swing.JLabel jLblPartidoElectoral;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JComboBox jcbBrillo;
    private javax.swing.JComboBox jcbContraste;
    private javax.swing.JComboBox jcbDispositivo;
    private javax.swing.JLabel lblImagen;
    private javax.swing.JTextField txtSalida;
    // End of variables declaration//GEN-END:variables

    private void dispSelect(int dispositivoseleccionado) {
        switch (dispositivoseleccionado) {
            case 0: //USB
            case 4: //CN_FDU02
            default:
                this.nombreDispositivo = FDxDeviceName.CN_FDU02;
                break;
            case 1: //Parallel
            case 5: //CN_FDP02
                this.nombreDispositivo = FDxDeviceName.CN_FDP02;
                break;
            case 2: //CN_FDU01
                this.nombreDispositivo = FDxDeviceName.CN_FDU01;
                break;
            case 3: //CN_FDP01
                this.nombreDispositivo = FDxDeviceName.CN_FDP01;
        }
    }

    private void activarBotones() {
        btnCaptura.setEnabled(true);
        btnLed.setEnabled(true);
        btnLed.setEnabled(true);
        btnfinalizar.setEnabled(true);
        jcbBrillo.setEnabled(true);
        jcbContraste.setEnabled(true);
        jcbBrillo.setSelectedIndex(deviceInfo.brightness);
        jcbContraste.setSelectedIndex(deviceInfo.contrast);
    }

    private void desactivarBotones() {
        btnCaptura.setEnabled(false);
        btnLed.setEnabled(false);
        btnLed.setEnabled(false);
        jcbBrillo.setEnabled(false);
        jcbContraste.setEnabled(false);
        btnfinalizar.setEnabled(false);
    }
}
